package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.util.StrUtil;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.SysConstant;
import com.tbit.uqbike.constant.enums.ParkPointType;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.DetentionArea;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.DetentionAreaService;
import com.tbit.uqbike.webmanager.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 扣押区
 *
 * @author yee_
 * @version 1.0
 * @date 2023/1/11 9:32
 */
@RestController
@RequestMapping("/detentionArea")
public class DetentionAreaController {

    @Autowired
    private DetentionAreaService detentionAreaService;

    /**
     * 获取区域所有扣押区
     *
     * @param accountId 区域id
     * @return .
     */
    @GetMapping("/getByAccountId")
    public Result<PageResult<DetentionArea>> getByAccountId(@RequestParam Integer accountId,
                                                            Integer page, Integer size, String name) {
        Assert.notNulls("分页参数不能为空", page, size);
        return detentionAreaService.listByAccountId(accountId, page, size, name);
    }

    /**
     * 根据扣押区id获取详情
     *
     * @param detentionId 。
     * @return 。
     */
    @GetMapping("/getByDetentionId")
    public Result<DetentionArea> getByDetentionId(@RequestParam Integer detentionId) {
        return detentionAreaService.getByDetentionId(detentionId);
    }

    /**
     * 新增
     *
     * @param detentionArea .
     * @param mapType       .
     * @return .
     */
    @PostMapping("/add")
    @OperateLog(module = ModuleEnum.MAINDETENTIONAREA, methods = MethodEnum.DETENTIONAREA_ADD)
    public Result<String> add(@RequestBody DetentionArea detentionArea, @RequestParam(required = false) Integer mapType) {
        DetentionArea detentionAreaByName = detentionAreaService.getByDetentionAreaName(detentionArea.getAccountId(), detentionArea.getName());
        if (detentionAreaByName != null) {
            throw new BaseException("该运营区内有重复的扣押区名称");
        }
        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }
        check(detentionArea);
        detentionAreaService.add(detentionArea, mapType);
        return Result.success();
    }

    /**
     * 更新
     *
     * @param detentionArea .
     * @param mapType       .
     * @return .
     */
    @PostMapping("/edit")
    @OperateLog(module = ModuleEnum.MAINDETENTIONAREA, methods = MethodEnum.DETENTIONAREA_UPDATE)
    public Result<String> edit(@RequestBody DetentionArea detentionArea, @RequestParam(required = false) Integer mapType) {
        DetentionArea detentionAreaByName = detentionAreaService.getByDetentionAreaName(detentionArea.getAccountId(), detentionArea.getName());
        if (detentionAreaByName != null && !Objects.equals(detentionArea.getDetentionId(), detentionAreaByName.getDetentionId())) {
            throw new BaseException("该运营区内有重复的扣押区名称");
        }
        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }
        Assert.notNull(detentionArea.getDetentionId(), "detentionArea.id.notNull");
        check(detentionArea);
        detentionAreaService.edit(detentionArea, mapType);
        return Result.success();
    }

    /**
     * 删除
     *
     * @param detentionId .
     * @return .
     */
    @DeleteMapping("/del")
    @OperateLog(module = ModuleEnum.MAINDETENTIONAREA, methods = MethodEnum.DETENTIONAREA_DEL)
    public Result<String> del(Integer detentionId) {
        boolean result = detentionAreaService.removeById(detentionId);
        return result ? Result.success() : Result.error();
    }

    /***
     * 前置非空校验
     * @param detentionArea .
     */
    void check(DetentionArea detentionArea) {
        Assert.notNull(detentionArea.getType(), "ParkPoint.type.notNull");
        Assert.hasLength(detentionArea.getName(), "ParkPoint.name.notNull");
        Assert.notNull(detentionArea.getAllowRange(), "ParkPoint.allowRange.notNull");
        Assert.notNull(detentionArea.getAccountId(), "AccountUser.agentId.notNull");
        if (detentionArea.getName().length() > 100){
            throw new BaseException("扣押区名称过长");
        }else if (StrUtil.isNotEmpty(detentionArea.getRemark()) && detentionArea.getRemark().length() > 255){
            throw new BaseException("扣押区备注过长");
        }
        if (ParkPointType.PARKPOINT_CIRCULAR_TYPE.getType().equals(detentionArea.getType().toString())) {
            Assert.notNull(detentionArea.getLo(), "Map.lon.nutNull");
            Assert.notNull(detentionArea.getLa(), "Map.lat.nutNull");
            detentionArea.setLoC(detentionArea.getLo());
            detentionArea.setLaC(detentionArea.getLa());
        } else if (ParkPointType.PARKPOINT_POLYGON_TYPE.getType().equals(detentionArea.getType().toString())) {
            Assert.notNull(detentionArea.getPoints(), "ParkPoint.points.notNull");
            // 保存高德地图原始经纬度
            detentionArea.setPointsC(detentionArea.getPoints());
        }
    }
}
